import 'package:flutter/material.dart';
import 'package:huobi/configs/app_style_config.dart';
import 'package:huobi/pages/assets/assets_page.dart';
import 'package:huobi/pages/contract/contract_page.dart';
import 'package:huobi/pages/main/home_drawer_page.dart';
import 'package:huobi/pages/home/home_page.dart';
import 'package:huobi/pages/market/market_page.dart';
import 'package:huobi/pages/trading/trading_page.dart';
import 'package:huobi/routers/hb_router.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {

  final GlobalKey<ScaffoldState> scaffoldKey =  GlobalKey<ScaffoldState>();


  List<Widget> pages;

  List<Map> navItems = [
    {"name":"首页","icon":Icons.home},
    {"name":"行情","icon":Icons.stacked_bar_chart},
    {"name":"交易","icon":Icons.shopping_cart},
    {"name":"合约","icon":Icons.insert_drive_file_outlined},
    {"name":"资产","icon":Icons.account_balance_wallet_rounded},
  ];

  List<BottomNavigationBarItem> bottomNavItems = List();
  PageController pageController = PageController();

  int currentIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initBottomNavItems();
    initPages();
  }

  // 初始化页面
  initPages(){
    pages = [
    HomePage(scaffoldKey: scaffoldKey,),
    MarketPage(),
    TradingPage(),
    ContractPage(),
    AssetsPage()
    ];
  }
  // 初始化NavItems
  initBottomNavItems(){
    navItems.forEach((element) {
      BottomNavigationBarItem itme =  BottomNavigationBarItem(
        icon: Icon(
          element["icon"],
        ),
        label:  element["name"],
        activeIcon: Icon(
          element["icon"],
        ),
      );
      bottomNavItems.add(itme);
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
          canvasColor: AppStyleConfig.themColor,
        ),
        child: BottomNavigationBar(
          unselectedItemColor: Colors.white,
          selectedItemColor: Colors.blue,
          showUnselectedLabels: true,
          items: bottomNavItems,
          type: BottomNavigationBarType.fixed,
          currentIndex: currentIndex,
          onTap: (index) {
            currentIndex = index;
            pageController.jumpToPage(currentIndex);
            setState(() {});
          },
        ),
      ),
      key: scaffoldKey,
      drawer: Drawer(
        child:Container(
          alignment: Alignment.center,
          child: HomeDrawerPage(pageTurnhandle: (path){
            HBRouter.navigateTo(context, path).then((value) => scaffoldKey.currentState.openDrawer());
          },),
        ),
      ),
      body: PageView(
          children: pages,
          physics: NeverScrollableScrollPhysics(),
          controller: pageController),
    );
  }
}
